#include "Common.h"
#include <QDir>
#include <QFile>

/**
 * @brief 读取文件文本
 * @param dir
 * @param file
 * @return
 */
QString Common::ReadAllText(const QString& dir, const QString& file)
{
    QDir dir_(dir);
    QFile file_(dir_.filePath(file));
    if (!file_.open(QIODevice::ReadOnly | QIODevice::Text)) return QString();
    QTextStream in(&file_);
    QString content = in.readAll();
    file_.close();
    return content;
}

/**
 * @brief 写入文件文本
 * @param dir
 * @param file
 * @param content
 * @return
 */
bool Common::WriteAllText(const QString& dir, const QString& file, const QString& content)
{
    QDir dir_(dir);
    QFile file_(dir_.filePath(file));
    if (!file_.open(QIODevice::WriteOnly | QIODevice::Text)) return false;
    file_.write(content.toUtf8());
    file_.close();
    return true;
}

/**
 * @brief 复制整个文件夹到指定目录
 * @param src
 * @param dst
 * @param exclude
 * @param overwrite
 * @return
 */
bool Common::CopyDirectory(const QString &src, const QString &dst, const QString &exclude, bool overwrite)
{
    QDir sourceDir(src);
    QDir targetDir(dst);
    if (!targetDir.exists() && !targetDir.mkdir(targetDir.absolutePath())) return false;
    QFileInfoList fileInfoList = sourceDir.entryInfoList();
    foreach (QFileInfo fileInfo, fileInfoList)
    {
        QString filename = fileInfo.fileName();
        if (filename == "." || filename == "..") continue;
        if (fileInfo.isDir()) CopyDirectory(fileInfo.filePath(), targetDir.filePath(filename), exclude, overwrite);
        else
        {
            if (!exclude.isEmpty() && filename.indexOf(exclude) >= 0) continue;
            if (targetDir.exists(filename))
            {
                if (overwrite) targetDir.remove(filename);
                else continue;
            }
            QFile::copy(fileInfo.filePath(), targetDir.filePath(filename));
        }
    }
    return true;
}

/**
 * @brief 列出目录下所有文件
 * @param basedir 根目录
 * @param dir 当前目录
 * @param files 文件列表，不含文件夹
 * @return
 */
void Common::ListAllFile(const QString& basedir, const QString& dir, QStringList& files) {
    QDir dir_(dir.isEmpty() ? basedir : dir);
    QDir base_(basedir);
    QFileInfoList fileInfoList = dir_.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
    foreach (auto fileInfo, fileInfoList) {
        if (fileInfo.isDir()) {
            ListAllFile(basedir, fileInfo.absoluteFilePath(), files);
        } else if (fileInfo.isFile()) {
            files.append(base_.relativeFilePath(fileInfo.absoluteFilePath()));
        }
    }
}

/**
 * @brief 转换颜色表示
 * @param color
 * @return
 */
QString Common::ConvertColor(const QVector3D& color) {
    int r = std::min(std::max(static_cast<int>(color.x() * 256), 0), 255);
    int g = std::min(std::max(static_cast<int>(color.y() * 256), 0), 255);
    int b = std::min(std::max(static_cast<int>(color.z() * 256), 0), 255);
    return QString("#%1%2%3FF")
        .arg(r, 2, 16, QLatin1Char('0'))
        .arg(g, 2, 16, QLatin1Char('0'))
        .arg(b, 2, 16, QLatin1Char('0')).toUpper();
}

/**
 * @brief 转换颜色表示
 * @param color
 * @return
 */
QVector3D Common::ConvertColor(const QString& color) {
    uint rgb = color.mid(1, 6).toUInt(nullptr, 16);
    float r = 0.0039f * ((rgb >> 16) & 0xFF);
    float g = 0.0039f * ((rgb >> 8) & 0xFF);
    float b = 0.0039f * (rgb & 0xFF);
    return QVector3D(r, g, b);
}
